import Vue from 'vue'
import Vuex from 'vuex'
import VueRouter from 'vue-router'
import home from '../components/home.vue'
import login from '../components/login.vue'
import welcome from '../components/welcome.vue'
import containerInfo from '../components/containerInfo'
import AutoDeploy from '../components/AutoDeploy'
import EcsInfo from '../components/EcsInfo'
import EnvironmentClean from '../components/EnvironmentClean'
import ProjectAdd from '../components/ProjectAdd'
import SlbInfo from '../components/SlbInfo'
import EcsExpand from '../components/EcsExpand'
import uidManage from '../components/uidManage'
import AttachContainer from '../components/container_attach'

Vue.use(Vuex)
Vue.use(VueRouter)

const timer = new Date().getTime()
const routes = [
  {
    path: '/',
    redirect: '/home',
    beforeEnter: (to, from, next) => {
      var isAuthenticated = Boolean(localStorage.getItem('isAuthenticated'))
      if (to.name !== 'login' && !isAuthenticated) next({ name: 'login' })
      else next()
    }
  },
  {
     name: 'login',
     path: '/login',
    component: login
  },
  {
    path: '/home',
    component: home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: welcome,
        beforeEnter: (to, from, next) => {
          var isAuthenticated = Boolean(localStorage.getItem('isAuthenticated'))
          if (localStorage.getItem('isAuthentime')) {
            var timeout = timer - localStorage.getItem('isAuthentime')
            if (timeout >= 172800000) {
              localStorage.removeItem('isAuthenticated')
              localStorage.removeItem('isAuthentime')
            }
          }
          if (to.name !== 'login' && !isAuthenticated) next({ name: 'login' })
          else next()
        }
      },
      {
        path: '/containerInfo',
        component: containerInfo,
        beforeEnter: (to, from, next) => {
          var isAuthenticated = Boolean(localStorage.getItem('isAuthenticated'))
          if (localStorage.getItem('isAuthentime')) {
            var timeout = timer - localStorage.getItem('isAuthentime')
            console.log(timeout)
            if (timeout >= 172800000) {
              localStorage.removeItem('isAuthenticated')
              localStorage.removeItem('isAuthentime')
            }
          }
          if (to.name !== 'login' && !isAuthenticated) next({ name: 'login' })
          else next()
        }
      },
      {
        path: '/autoDeploy',
        component: AutoDeploy,
        beforeEnter: (to, from, next) => {
          var isAuthenticated = Boolean(localStorage.getItem('isAuthenticated'))
          if (localStorage.getItem('isAuthentime')) {
            var timeout = timer - localStorage.getItem('isAuthentime')
            if (timeout >= 172800000) {
              localStorage.removeItem('isAuthenticated')
              localStorage.removeItem('isAuthentime')
            }
          }
          if (to.name !== 'login' && !isAuthenticated) next({ name: 'login' })
          else next()
        }
      },
      {
        path: '/EcsInfo',
        component: EcsInfo,
        beforeEnter: (to, from, next) => {
          var isAuthenticated = Boolean(localStorage.getItem('isAuthenticated'))
          if (localStorage.getItem('isAuthentime')) {
            var timeout = timer - localStorage.getItem('isAuthentime')
            if (timeout >= 172800000) {
              localStorage.removeItem('isAuthenticated')
              localStorage.removeItem('isAuthentime')
            }
          }
          if (to.name !== 'login' && !isAuthenticated) next({ name: 'login' })
          else next()
        }
      },
      {
        path: '/EnvironmentClean',
        component: EnvironmentClean,
        beforeEnter: (to, from, next) => {
          var isAuthenticated = Boolean(localStorage.getItem('isAuthenticated'))
          if (localStorage.getItem('isAuthentime')) {
            var timeout = timer - localStorage.getItem('isAuthentime')
            if (timeout >= 172800000) {
              localStorage.removeItem('isAuthenticated')
              localStorage.removeItem('isAuthentime')
            }
          }
          if (to.name !== 'login' && !isAuthenticated) next({ name: 'login' })
          else next()
        }
      },
      {
        path: '/ProjectAdd',
        component: ProjectAdd,
        beforeEnter: (to, from, next) => {
          var isAuthenticated = Boolean(localStorage.getItem('isAuthenticated'))
          if (localStorage.getItem('isAuthentime')) {
            var timeout = timer - localStorage.getItem('isAuthentime')
            if (timeout >= 172800000) {
              localStorage.removeItem('isAuthenticated')
              localStorage.removeItem('isAuthentime')
            }
          }
          if (to.name !== 'login' && !isAuthenticated) next({ name: 'login' })
          else next()
        }
      },
      {
        path: '/SlbInfo',
        component: SlbInfo,
        beforeEnter: (to, from, next) => {
          var isAuthenticated = Boolean(localStorage.getItem('isAuthenticated'))
          if (localStorage.getItem('isAuthentime')) {
            var timeout = timer - localStorage.getItem('isAuthentime')
            if (timeout >= 172800000) {
              localStorage.removeItem('isAuthenticated')
              localStorage.removeItem('isAuthentime')
            }
          }
          if (to.name !== 'login' && !isAuthenticated) next({ name: 'login' })
          else next()
        }
      },
      {
        path: '/EcsExpand',
        component: EcsExpand,
        beforeEnter: (to, from, next) => {
          var isAuthenticated = Boolean(localStorage.getItem('isAuthenticated'))
          if (localStorage.getItem('isAuthentime')) {
            var timeout = timer - localStorage.getItem('isAuthentime')
            if (timeout >= 172800000) {
              localStorage.removeItem('isAuthenticated')
              localStorage.removeItem('isAuthentime')
            }
          }
          if (to.name !== 'login' && !isAuthenticated) next({ name: 'login' })
          else next()
        }
      },
      {
        path: '/uidManage',
        component: uidManage,
        beforeEnter: (to, from, next) => {
          var isAuthenticated = Boolean(localStorage.getItem('isAuthenticated'))
          if (localStorage.getItem('isAuthentime')) {
            var timeout = timer - localStorage.getItem('isAuthentime')
            if (timeout >= 172800000) {
              localStorage.removeItem('isAuthenticated')
              localStorage.removeItem('isAuthentime')
            }
          }
          if (to.name !== 'login' && !isAuthenticated) next({ name: 'login' })
          else next()
        }
      },
      {
        path: '/AttachContainer',
        component: AttachContainer,
        beforeEnter: (to, from, next) => {
          var isAuthenticated = Boolean(localStorage.getItem('isAuthenticated'))
          if (localStorage.getItem('isAuthentime')) {
            var timeout = timer - localStorage.getItem('isAuthentime')
            if (timeout >= 172800000) {
              localStorage.removeItem('isAuthenticated')
              localStorage.removeItem('isAuthentime')
            }
          }
          if (to.name !== 'login' && !isAuthenticated) next({ name: 'login' })
          else next()
        }
      }
    ]
  }
  // {
  //   path: '/test1',
  //   name: 'test1',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../components/welcome.vue')
  // }
]

const router = new VueRouter({
  routes
})

export default router
